// pages/operation/followUpManage/followUpManageIndex/index.js
import {
  FollowUpModel
} from "../../../../api/api-work/followUpModel.js";
import Dialog from '@vant/weapp/dialog/dialog';
const followUpModel = new FollowUpModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cellId: "",
    activeTab: "first",
    wxParam: "",
    followUptriggered: false,
    isShowFollowUpDetail: false, // 是否显示跟进详情
    mine: "0", // 全部 1：我的
    pageNum: 1,
    total: 0,
    tableData: [],
    loadStatus: "",
    name: "", // 姓名
    contactPhone: "", // 电话
    contactName: "", // 跟进方式
    phaseName: "", // 跟进阶段
    counselor: "", // 跟进人
    contactTime: "", // 跟进时间
    selectRow: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.setData({
      pageNum: 1,
      tableData: []
    })
    await this.getFollowupData();
  },

  onTabChange(e) {
    if (e.detail.name == "first") {
      // 全部跟进
      this.setData({
        mine: "0",
        pageNum: 1,
        tableData: []
      })
    } else {
      // 我的跟进
      this.setData({
        mine: "1",
        pageNum: 1,
        tableData: []
      })
    }
    this.getFollowupData()
  },

  searchFollowUp() {
    this.setData({
      pageNum: 1,
      tableData: []
    })
    this.getFollowupData()
  },

  // 获取跟进管理列表
  async getFollowupData() {
    // mine: 0:全部 1：我的   type:4 
    followUpModel.getFollowListApi({
      wxParam: this.data.wxParam,
      pageNum: this.data.pageNum,
      pageSize: 10,
      mine: this.data.mine,
      stage: 3
    }).then(res => {
      let rows = this.data.tableData;
      rows.push(...res.rows)
      this.setData({
        tableData: rows,
        total: res.total
      })
      if (this.data.total == 0) {
        this.setData({
          // 无数据
          loadStatus: ""
        })
      } else if (this.data.tableData.length < this.data.total) {
        // 还有数据
        this.setData({
          // 加载更多
          loadStatus: "loadmore"
        })
      } else {
        // 没有数据了
        this.setData({
          // 加载更多
          loadStatus: "nomore"
        })
      }
    })
  },

  getMoreFollowup() {
    if (this.data.loadStatus === "nomore") {
      return
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading"
    })
    this.getFollowupData()
  },
  refreshMoreFollowup() {
    this.setData({
      followUptriggered: false,
      pageNum: 1,
      tableData: []
    })
    this.getFollowupData()
  },

  // 添加跟进
  addFollowRecord(e) {
    const item = e.currentTarget.dataset.query;
    wx.navigateTo({
      url: `/pages/operation/followUpManage/addFollowRecord/index?studentId=${item.studentId}`,
    })
  },

  // 删除跟进
  deleteFollowRecord(e) {
    console.log(e);
    const query = e.currentTarget.dataset.query
    Dialog.confirm({
      title: '确认删除？',
    })
      .then(() => {

        followUpModel.deleteFollowApi({
          id: query.id
        }).then(res => {
          this.setData({
            isShowFollowUpDetail: false,
          })
          this.searchFollowUp()
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  // 点击跟进
  tapFollowUp(e) {

    const query = e.currentTarget.dataset.query;
    this.setData({
      selectRow: query,
      cellId: query.id,
      isShowFollowUpDetail: true,
      name: query.name,
      contactPhone: query.contactPhone, // 电话
      contactName: query.contactName, // 跟进方式
      phaseName: query.phaseName, // 跟进阶段
      counselor: query.counselor, // 跟进人
      contactTime: query.contactTime, // 跟进时间
    })
  },

  // 关闭跟进弹窗
  onCloseFollowUp() {
    this.setData({
      isShowFollowUpDetail: false
    })
  },

  //跟进详情
  toDetails() {
    this.setData({
      isShowFollowUpDetail: false
    })
    const data = JSON.stringify({ ...this.data.selectRow, id: this.data.selectRow.studentId });
    wx.navigateTo({
      url: `/pages/operation/clueManage/clueDetail/index?obj=${data}`,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})